﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using TomNet.Core;
using TomNet.App.Core.Contracts;
using TomNet.App.Model.DB;
using TomNet.App.Model.DTO;
using TomNet.AspNetCore.Data;
using TomNet.SqlSugarCore.Entity;
using Microsoft.Extensions.Logging;

namespace TomNet.App.Web.Controllers
{
    [Authorize]
    [Description("个人中心")]
    public class MyController : Common.BaseController
    {
        private readonly IBusinessContract _businessContract;

        public MyController(IBusinessContract businessContract)
        {
            _businessContract = businessContract;
        }

        public IActionResult Business()
        {
            return View();
        }

        public IActionResult CloseLog()
        {
            return View();
        }

        #region IActionResult CloseLogAsync(int businessId = -1,int page = 1, int limit = 20)

        [HttpPost]
        public IActionResult CloseLogAsync(int businessId = -1, int page = 1, int limit = 20)
        {
            var query = _businessContract.UnitOfWork.DbContext.Queryable<Model.DB.BusinessOnOffRunnings, Model.DB.Business, Model.DB.Identity.Users>(
                (a, b, c) => a.BusinessId == b.Id && b.UserId == c.Id)
                .Where((a, b, c) => a.State == 1)
                .Where((a, b, c) => b.UserId == LoginUserId && ((businessId == -1 && a.Id == businessId)))
                .OrderBy((a, b, c) => a.OffTime)
                .Select((a, b, c) => new CloseLogAsyncModel
                {
                    Id = a.Id,
                    UserName = c.UserName,
                    BusinessId = a.BusinessId,
                    ProductName = b.ProductName,
                    OffTime = a.OffTime.ToString("yyyy-MM-dd HH:mm:ss")
                });

            int total = 0;
            var list = query.ToPageList(page, limit, ref total);

            var result = new LayuiPageResult<CloseLogAsyncModel>
            {
                Code = 0,
                Msg = "Success",
                Count = total,
                Data = list.ToArray()
            };
            return Json(result);
        }

        public class CloseLogAsyncModel
        {
            public int Id { get; set; }

            public string UserName { get; set; }

            public int BusinessId { get; set; }

            public string ProductName { get; set; }

            public string OffTime { get; set; }
        }

        #endregion
    }
}